package com.example.zingalbumdownloader.utility;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

import com.example.zingalbumdownloader.model.Album;

public class ZingMp3Parser_Playlist {
	private static final String ns = null;

	public void parse(String in, List<Album> list)
			throws XmlPullParserException, IOException {
		XmlPullParser parser = Xml.newPullParser();
		parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
		StringReader reader = new StringReader(in);
		parser.setInput(reader);
		parser.nextTag();
		readhtml(parser, list);
	}

	private void readhtml(XmlPullParser parser, List<Album> list)
			throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, ns, "html");
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			if (name.equals("div")) {
				readDivLv1(parser, list);
			} else {
				skip(parser);
			}
		}
	}

	private void readDivLv1(XmlPullParser parser, List<Album> list)
			throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, ns, "div");
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			if (name.equals("div")
					&& parser.getAttributeValue(ns, "class").equals(
							"video-item")) {
				readDivLv2(parser, list);
			} else {
				skip(parser);
			}
		}
	}

	private void readDivLv2(XmlPullParser parser, List<Album> list)
			throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, ns, "div");
		Album album;
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			StringBuilder href = new StringBuilder(MyResource.http_mp3_zing_vn);
			if (name.equals("a")
					&& parser.getAttributeValue(ns, "class")
							.equals("album-img")) {
				href.append(parser.getAttributeValue(ns, "href"));
				album = new Album("", href.toString(), "");
				readImg(parser, album);
				list.add(album);
			} else {
				skip(parser);
			}
		}
	}

	private void readImg(XmlPullParser parser, Album album)
			throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, ns, "a");
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			if (name.equals("img")) {
				readTitle_Src(parser, album);
			} else {
				skip(parser);
			}
		}
	}

	private void readTitle_Src(XmlPullParser parser, Album album)
			throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, ns, "img");
		String tag = parser.getName();
		if (tag.equals("img")) {
			album.setTitle(parser.getAttributeValue(null, "alt"));
			album.setSrc_pic(parser.getAttributeValue(null, "src"));
			parser.nextTag();
		}
		parser.require(XmlPullParser.END_TAG, ns, "img");
	}

	private void skip(XmlPullParser parser) throws XmlPullParserException,
			IOException {
		if (parser.getEventType() != XmlPullParser.START_TAG) {
			throw new IllegalStateException();
		}
		int depth = 1;
		while (depth != 0) {
			switch (parser.next()) {
			case XmlPullParser.END_TAG:
				depth--;
				break;
			case XmlPullParser.START_TAG:
				depth++;
				break;
			}
		}
	}

}
